package edu.zju.gis.cky.util;

import java.sql.*;

public class PgHelper {

    private final String url;
    private final String username;
    private final String password;
    private Connection connection;

    public PgHelper(String url, String username, String password) {
        this.url = url;
        this.username = username;
        this.password = password;
    }

    public void init() {
        try {
            Class.forName("org.postgresql.Driver").newInstance();
            connection = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public ResultSet query(String sql) {
        try {
            PreparedStatement pStatement = this.connection.prepareStatement(sql);
            return pStatement.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

}
